-
Notifications
You must be signed in to change notification settings - Fork 361
Add support for generic constraints to Actor source generator #1535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Signed-off-by: Oisin Grehan <[email protected]>
aa000f9
to
087f11b
Compare
Signed-off-by: Oisin Grehan <[email protected]>
1fb41e7
to
435933c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds support for generic type constraints to the Actor source generator, enabling it to properly handle and generate constraint clauses for generic actor interfaces. The generator now correctly preserves constraints like where T: class
, where T: struct
, where T: unmanaged
, interface constraints, where T: new()
, and where T: notnull
when creating actor client classes.
- Implemented constraint clause generation for all supported generic constraint types
- Added logic to iterate through type parameters and build appropriate constraint syntax
- Added comprehensive test coverage for both single and multiple type parameter scenarios
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
src/Dapr.Actors.Generators/ActorClientGenerator.cs | Implements constraint clause generation logic for generic type parameters in the actor client generator |
test/Dapr.Actors.Generators.Test/ActorClientGeneratorTests.cs | Adds test cases for single and multiple type parameter constraints to verify the generator works correctly |
// Add unmanaged constraint | ||
if (typeParam.HasUnmanagedTypeConstraint) | ||
{ | ||
constraints.Add(SyntaxFactory.TypeConstraint(SyntaxFactory.IdentifierName("unmanaged"))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The unmanaged constraint should be added before type constraints but after class/struct constraints. The current order may not follow C# constraint ordering requirements where 'unmanaged' should come before interface constraints.
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reevaluate this and suggest a fix using the information at https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters
Co-authored-by: Copilot <[email protected]> Signed-off-by: Oisin Grehan <[email protected]>
Description
The actor source generator will now correctly add any type constraints to the client class. Supported:
<class or interface>
...and any legal combinations thereof.
I've added tests for a single case (single type parameter and one constraint) and a complex case (multiple type parametes and constraints.)
Issue reference
We strive to have all PR being opened based on an issue, where the problem or feature have been discussed prior to implementation.
Please reference the issue this PR will close: #1534
Checklist
Please make sure you've completed the relevant tasks for this PR, out of the following list: